package com.xiaoyu.lanling.feature.visitor.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.xiaoyu.base.data.UserData
import com.xiaoyu.base.event.AppEventBus
import com.xiaoyu.base.event.SimpleEventHandler
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.common.Constant
import com.xiaoyu.lanling.event.vip.VipTypeUpdateEvent
import com.xiaoyu.lanling.feature.user.model.UserExtra
import com.xiaoyu.lanling.feature.visitor.controller.VisitorListViewController
import com.xiaoyu.lanling.feature.visitor.data.VisitorListViewData
import com.xiaoyu.lanling.media.image.ImageViewUtil
import com.xiaoyu.lanling.router.Router
import com.xiaoyu.lanling.view.OnShowFragment
import kotlinx.android.synthetic.main.fragment_visitor.*
import kotlinx.android.synthetic.main.visitor_permission_not_granted.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class VisitorFragment() : OnShowFragment() {

    lateinit var listData: VisitorListViewData
    lateinit var controller: VisitorListViewController

    companion object {

        private val KEY_VISITOR_COUNT = "key_visitor_count"

        fun newInstance(visitorCount: Int): VisitorFragment {
            val args = Bundle()
            args.putInt(KEY_VISITOR_COUNT, visitorCount)

            val fragment = VisitorFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_visitor, container, false)
    }

    override fun onViewCreatedSafelyAfterAppFinishInit(view: View, savedInstanceState: Bundle?) {
        initBind()
        initData()
        initEvent()
    }

    private fun initBind() {
        open_vip_btn.setOnClickListener {
            context?.let {
                Router.instance.gotoVipProductListActivity(it, Constant.From.VISITOR)
            }
        }
    }

    private fun initEvent() {
        AppEventBus.bindContainerAndHandler(this, object : SimpleEventHandler() {
            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: VipTypeUpdateEvent) {
                getVipStatus()
            }
        })
    }

    private fun initData() {
        listData = VisitorListViewData()
        context?.let {
            controller = VisitorListViewController.create(it, listData, visitor_swipe_refresh_layout, visitor_recyclerview)
        }
        getVipStatus()
    }

    private fun getVipStatus() {
        if (UserExtra.instance.isVip) {
            visitor_permission_layout.visibility = View.GONE
        } else {
            val count = arguments?.getInt(KEY_VISITOR_COUNT, 0)

            count?.let {
                UserData.getInstance().user ?: return@let
                visitor_permission_layout.visibility = View.VISIBLE
                visitor_count.text = it.toString()
                name.text = UserData.getInstance().user.name
                ImageViewUtil.loadImage(avatar, UserData.getInstance().user, 148, true)
            }
        }
        controller.loadFirstPageData()
    }
}